import Vue from 'vue';
import NotifyComponent from './Notify.vue';

const NotifyConstructor = Vue.extend(NotifyComponent);
let currentNotify = null;

const Notify = (options = {}) => {
  if (currentNotify) currentNotify.visible = false;

  const opts = typeof options === 'string' ? { message: options } : options;

  currentNotify = new NotifyConstructor({ propsData: opts });
  currentNotify.$mount();
  document.body.appendChild(currentNotify.$el);

  Vue.nextTick(() => {
    currentNotify.visible = true;
  });

  return currentNotify;
};

['primary', 'success', 'danger', 'warning'].forEach(type => {
  Notify[type] = (message, options = {}) =>
    Notify({ ...options, type, message });
});

Vue.prototype.$notify = Notify;

export default Notify;